package cn.china.myspringboot.controller;


import cn.china.myspringboot.config.JwtConfig;
import cn.china.myspringboot.entity.Result;
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class TokenController {

    @Autowired
    JwtConfig jwtConfig;

@RequestMapping("/login")
    public JSONObject login(String username,String password){


      //自己进数据验证用户名和密码
      //登录成功后生成token
   String token= jwtConfig.createToken(username);
    System.out.println(token);
    JSONObject jsonObject=new JSONObject();
    jsonObject.put(jwtConfig.getHeader(),token);
    return Result.success(jsonObject);
}


    @RequestMapping("/select")
    public JSONObject select(){
    JSONObject jsonObject=new JSONObject();
    jsonObject.put("info","hello");
    return Result.success(jsonObject);
}


    @RequestMapping("/getUserName")
    public void getUserName(){
        System.out.println("getUserName");
        String token="eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFM1MTIifQ.eyJzdWIiOiJ0b20iLCJpYXQiOjE2NzI4ODg4MjUsImV4cCI6MTY3Mjg5MjQyNX0.2ZXLyH_5ea7DKMzViYyM7MCD08UMg5V_dIY6QzwItdmvfZmpR1LyBtpoNqV1mk0Qoe7-rCEVjXUGQJWilLkIsw";

        Claims claims=jwtConfig.getTokenClaim(token);
        System.out.println(claims.getSubject());
       // System.out.println(claims.getSubject());

}

}
